<?php
class category_labelModel extends baseModel{
    protected $table = 'category';

    public function category_list($data){
        $where=array();
        if(empty($data['show'])){
            $where['show']=' `show`=1 ';
        }
        if(!empty($data['mid'])){
            $where['mid'] = ' AND mid in(' . $data['mid'] . ')';
        }
        if(!empty($data['cid'])){
            $where['cid'] = ' AND cid in(' . $data['cid'] . ')';
        }
        if(!empty($data['pid'])){
            $where['pid'] = ' AND pid in(' . $data['pid']. ')';
        }
        if ($data['type'] == 'top') {
            $where['pid'] = ' AND pid=0';
        }
        if(!empty($data['type'])&&!empty($data['pid'])){
            $where['pid'] = ' AND pid in (' . api('default','category_getcat',$data['pid']) . ')';
        }
        if(!empty($data['att'])){
            if($data['att']=='list'){
                $where['type'] = ' AND type=1 ';
            }else{
                 $where['type'] = ' AND type=0 ';
            }
        }
        if (!empty($data['where'])) {
            $where['where'] = ' AND '.$data['where'];
        }
        if (!empty($data['limit'])) {
            $where['limit'] = intval($data['limit']);
        }else{
            $where['limit'] = 10;
        }
        if (!empty($data['order'])) {
            $where['order'] = $data['order'];
        }else{
            $where['order'] = 'cid desc';
        }

        $condition=$where['show'].$where['mid'].$where['cid'].$where['pid'].$where['type'].$where['where'];
        $data=$this->model->table('category')->where($condition)->limit($where['limit'])->order($where['order'])->select();
        if(!empty($data)){
            $i=0;
            foreach ($data as $key=>$value) {
                $i++;
                $list[$key]=$value;
                $list[$key]['curl']=$this->get_curl($value);
                $list[$key]['i']=$i;
            }
        }
        return $list;
    }

    //栏目超链接
    public function get_curl($data,$page=false)
    {
        if(!is_array($data)){
            $data=$this->find('cid='.$data);
        }
        $model=$this->model->table('model')->where('mid='.$data['mid'])->find();
        if(empty($model)){
            return;
        }
        $config=config('APP');
        if($config['URL_REWRITE_ON']){
            $rewrite = config('REWRITE');
            $rewrite = array_flip($rewrite);
            $url=$rewrite[$model['model'].'/category/index'];
            $parameter=array('cid'=>$data['cid']);
            if(!empty($url)){
                $parameter=array();
                if(strpos($url,'<cid>')){
                    $parameter['cid']=$data['cid'];
                }
                if(strpos($url,'<urlname>')){
                    $parameter['urlname']=$data['urlname'];
                }
            }
        }else{
            $parameter=array('cid'=>$data['cid']);
        }
        if($page){
            $pagearray=array('page'=>'{page}');
            $parameter=array_merge($parameter,$pagearray);
        }
        return url($model['model'].'/category/index',$parameter);
    }


}

?>